package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Damagelist;
import com.jxc.model.po.DamagelistItem;
import com.jxc.model.po.Goods;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VDamagelistItem;
import com.jxc.service.DamagelistItemServiceI;

@Service("damagelistItemService")
public class DamagelistItemServiceImpl implements DamagelistItemServiceI {

	@Autowired
	private BaseDaoI<DamagelistItem> damagelistItemDao;

	@Override
	public List<VDamagelistItem> combobox(VDamagelistItem vdli) {
		return this.changeModel(this.find(vdli));
	}

	@Override
	public DataGrid datagrid(VDamagelistItem vdli) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vdli)));
		dg.setTotal(this.total(vdli));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					DamagelistItem dli = damagelistItemDao.get(DamagelistItem.class, Long.parseLong(id.trim()));
					if (dli != null) {
						damagelistItemDao.delete(dli);
					}
				}
			}
		}
	}

	@Override
	public void save(VDamagelistItem vdli) {
		DamagelistItem dli = new DamagelistItem();
		BeanUtils.copyProperties(vdli, dli);
		if (vdli.getGid() != null) {
			dli.setGoods(new Goods(vdli.getGid()));
		}
		if (vdli.getDlid() != null) {
			dli.setDamagelist(new Damagelist(vdli.getDlid()));
		}
		this.damagelistItemDao.save(dli);
	}

	@Override
	public void saveOrUpdate(VDamagelistItem vdli) {
		if (vdli.getId() != null) {
			this.update(vdli);
		} else {
			this.save(vdli);
		}
	}

	@Override
	public void update(VDamagelistItem vdli) {
		DamagelistItem dli = damagelistItemDao.get(DamagelistItem.class, vdli.getId());
		BeanUtils.copyProperties(vdli, dli, new String[] { "id" });
		if (vdli.getGid() != null) {
			dli.setGoods(new Goods(vdli.getGid()));
		}
		if (vdli.getDlid() != null) {
			dli.setDamagelist(new Damagelist(vdli.getDlid()));
		}
		this.damagelistItemDao.update(dli);
	}

	private List<VDamagelistItem> changeModel(List<DamagelistItem> dlis) {
		List<VDamagelistItem> vdlis = new ArrayList<VDamagelistItem>();
		if (dlis != null && dlis.size() > 0) {
			for (DamagelistItem dli : dlis) {
				VDamagelistItem vdli = new VDamagelistItem();
				BeanUtils.copyProperties(dli, vdli);
				if (dli.getGoods() != null) {
					Goods goods = dli.getGoods();
					vdli.setGid(goods.getId());
					vdli.setGname(goods.getName());
					Hibernate.initialize(goods.getBrand());
					vdli.setBname(goods.getBrand().getName());
					vdli.setType(goods.getType());
					vdli.setUnit(goods.getUnit());
					vdli.setSize(goods.getSize());
					vdli.setColor(goods.getColor());
					vdli.setShowPrice(goods.getShowPrice());
				}
				if (dli.getDamagelist() != null) {
					Damagelist dl = dli.getDamagelist();
					vdli.setDlid(dl.getId());
				}
				vdlis.add(vdli);
			}
		}
		return vdlis;
	}

	private List<DamagelistItem> find(VDamagelistItem vdli) {
		String hql = "from DamagelistItem t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vdli, hql, values);

		if (vdli.getSort() != null && vdli.getOrder() != null) {
			hql += " order by " + vdli.getSort() + " " + vdli.getOrder();
		}
		return damagelistItemDao.find(hql, values, vdli.getPage(), vdli.getRows());
	}

	private Long total(VDamagelistItem vdli) {
		String hql = "select count(*) from DamagelistItem t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vdli, hql, values);
		return damagelistItemDao.count(hql, values);
	}

	private String addWhere(VDamagelistItem vdli, String hql, List<Object> values) {
		if (vdli.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vdli.getId());
		}

		if (vdli.getGid() != null) {
			hql += " and t.goods.id = ? ";
			values.add(vdli.getGid());
		}

		if (vdli.getDlid() != null) {
			hql += " and t.damagelist.id = ? ";
			values.add(vdli.getDlid());
		}

		return hql;
	}

}
